﻿using UnityEngine;
using System.Collections;

public class QuestMainSceneManager : Scene {
	public UILabel QuestBigTitleLabel;
	public UILabel QuestTitleLabel;
	public UILabel QuestBodyLabel;

	public UISprite rewardItemImage;
    public UILabel rewardItemName;
    public GameObject[] rewardItemRareStar;
	public UILabel[] rewardAmountLabel;
	public Transform[] rewardCellObject;

	public GameObject hintObject;
	public UILabel hintLabel;

	public GameObject goDirectButtonObject;
	public GameObject questCompleteButtonObject;

    public GameObject rewardCellGridObject;

    public GameObject completeMarkObject;

    public UILabel hintTextLabelObject;
    public Transform hintButtonIconTranform;

	// Use this for initialization
	public override void StartScene()
    {
    	base.StartScene();

        if (PlayerPrefs.GetInt("questVisit") == 0)
            PlayerPrefs.SetInt("questVisit", 1);
        ToastManager.Instance.isOnGoingToQuestScene = false;
        SetActiveHelpButton(false);
        Vector3 temp = hintButtonIconTranform.localPosition;
        temp.x = -hintTextLabelObject.width / 2;
        hintButtonIconTranform.localPosition = temp;
#if UNITY_EDITOR
        if (UserData.Instance.userModel != null)
            SetQuestInfos();
        else
        {
            SoundController.Create();
            ResourceManager.Inst.PatchDataFile(() =>
            {
                GameData.Init();
                //TextData.Init();
                EventData.Init();
                CashData.Init();
                //UserData.Instance.m_Language = Language.text_ko;
            });

            NetworkManager.Instance.C2S_LOGIN(AuthPlatformID.None, "shinpc01", string.Empty, result =>
            {
                UserData.Instance.userModel = result.data;
                SetQuestInfos();
            }, null);
        }
#else
        SetQuestInfos();
#endif
	}

	public void SetQuestInfos()
	{
		int questId = UserData.Instance.userModel.questInfo.questId;

		DQuestInfo questInfo = GameData.Inst.GetQuestInfo(questId);
		QuestBigTitleLabel.text = TextController.GetStringWithId(string.Format("questTitle{0}", questId));
        string questObjectiveTitle = TextController.GetStringWithId(string.Format("questObjective{0}", questInfo.objective)).Replace("{0}", questInfo.value1.ToString());
        if( questInfo.objective == 81 )
            questObjectiveTitle = questObjectiveTitle.Replace("{1}", TextController.GetStringWithId(string.Format("storeCategory{0}", questInfo.value2)));
        else
            questObjectiveTitle = questObjectiveTitle.Replace("{1}", TextController.GetStringWithId(string.Format("region{0}", questInfo.value2)));
		QuestTitleLabel.text = string.Format("{0} ({1}/{2})", questObjectiveTitle, UserData.Instance.userModel.questInfo.value, questInfo.value1);
		QuestBodyLabel.text = TextController.GetStringWithId(string.Format("questScript{0}", questId));
		QuestBodyLabel.ResizeCollider();

        Vector3 pos = rewardCellGridObject.transform.parent.localPosition;
        pos.y = -QuestBodyLabel.height - 300f;
        if (pos.y > -550f)
        {
            pos.y = -550f;
        }
        rewardCellGridObject.transform.parent.localPosition = pos;

		if( questInfo.reward1Type != 0 )
		{
			if( questInfo.reward1Type < 3 )
				rewardAmountLabel[questInfo.reward1Type-1].text = questInfo.reward1Value1.ToString();
			else if( questInfo.reward1Type == 4 )
			{
                //rewardItemImage.spriteName = TextController.GetItemDefine(questInfo.reward1Value1);
                WeaponAtlasController.SetWeaponSprite(rewardItemImage, questInfo.reward1Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward1Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward1Value1)].SetActive(true);
                if (questInfo.reward1Value2 > 1)
                    rewardAmountLabel[questInfo.reward1Type - 1].text = string.Format("x{0}", questInfo.reward1Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward1Type - 1].gameObject.SetActive(false);
			}
			rewardCellObject[questInfo.reward1Type-1].FindChild("Off").gameObject.SetActive(false);
			rewardCellObject[questInfo.reward1Type-1].FindChild("On").gameObject.SetActive(true);
            rewardCellObject[questInfo.reward1Type - 1].gameObject.SetActive(true);
		}

		if( questInfo.reward2Type != 0 )
		{
			if( questInfo.reward2Type < 3 )
				rewardAmountLabel[questInfo.reward2Type-1].text = questInfo.reward2Value1.ToString();
            else if (questInfo.reward2Type == 4)
			{
                //rewardItemImage.spriteName = TextController.GetItemDefine(questInfo.reward2Value1);
                WeaponAtlasController.SetWeaponSprite(rewardItemImage, questInfo.reward2Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward2Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward2Value1)].SetActive(true);
                if (questInfo.reward2Value2 > 1)
                    rewardAmountLabel[questInfo.reward2Type - 1].text = string.Format("x{0}", questInfo.reward2Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward2Type - 1].gameObject.SetActive(false);
			}
			rewardCellObject[questInfo.reward2Type-1].FindChild("Off").gameObject.SetActive(false);
			rewardCellObject[questInfo.reward2Type-1].FindChild("On").gameObject.SetActive(true);
            rewardCellObject[questInfo.reward2Type - 1].gameObject.SetActive(true);
		}

		if( questInfo.reward3Type != 0 )
		{
			if( questInfo.reward3Type < 3 )
				rewardAmountLabel[questInfo.reward3Type-1].text = questInfo.reward3Value1.ToString();
            else if (questInfo.reward3Type == 4)
			{
                //rewardItemImage.spriteName = TextController.GetItemDefine(questInfo.reward3Value1);
                WeaponAtlasController.SetWeaponSprite(rewardItemImage, questInfo.reward3Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward3Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward3Value1)].SetActive(true);
                if (questInfo.reward3Value2 > 1)
                    rewardAmountLabel[questInfo.reward3Type - 1].text = string.Format("x{0}", questInfo.reward3Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward3Type - 1].gameObject.SetActive(false);
			}
			rewardCellObject[questInfo.reward3Type-1].FindChild("Off").gameObject.SetActive(false);
			rewardCellObject[questInfo.reward3Type-1].FindChild("On").gameObject.SetActive(true);
            rewardCellObject[questInfo.reward3Type - 1].gameObject.SetActive(true);
		}

		if( questInfo.reward4Type != 0 )
		{
			if( questInfo.reward4Type < 3 )
				rewardAmountLabel[questInfo.reward4Type-1].text = questInfo.reward4Value1.ToString();
            else if (questInfo.reward4Type == 4)
			{
                //rewardItemImage.spriteName = TextController.GetItemDefine(questInfo.reward4Value1);
                WeaponAtlasController.SetWeaponSprite(rewardItemImage, questInfo.reward4Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward4Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward4Value1)].SetActive(true);
                if (questInfo.reward4Value2 > 1)
                    rewardAmountLabel[questInfo.reward4Type - 1].text = string.Format("x{0}", questInfo.reward4Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward4Type - 1].gameObject.SetActive(false);
			}
			rewardCellObject[questInfo.reward4Type-1].FindChild("Off").gameObject.SetActive(false);
			rewardCellObject[questInfo.reward4Type-1].FindChild("On").gameObject.SetActive(true);
            rewardCellObject[questInfo.reward4Type - 1].gameObject.SetActive(true);
		}

		hintLabel.text = TextController.GetStringWithId(string.Format("questHint{0}", questId));

		if( UserData.Instance.userModel.questInfo.state == 4 )
		{
            completeMarkObject.SetActive(true);
			goDirectButtonObject.SetActive(false);
			questCompleteButtonObject.SetActive(true);
		}

        rewardCellGridObject.GetComponent<UIGrid>().Reposition();
	}

	public void OpenHintWindow()
	{
		hintObject.transform.FindChild("on").gameObject.SetActive(true);
		hintObject.transform.FindChild("off").gameObject.SetActive(false);
	}

	public void HideHintWindow()
	{
		hintObject.transform.FindChild("on").gameObject.SetActive(false);
		hintObject.transform.FindChild("off").gameObject.SetActive(true);
	}

	public void OnClickedGoDirectButton()
	{
        DQuestInfo questInfo = GameData.Inst.GetQuestInfo(UserData.Instance.userModel.questInfo.questId);

        switch (questInfo.objective)
        {
            case 1:
            case 2:
            case 11:
            case 21:
                SceneController.LoadLevel("Character_Info");
                break;
            case 31:
            case 32:
            case 33:
            case 34:
            case 35:
            case 36:
                SceneController.LoadLevel("World_GStar");
                break;
            case 41:
            case 42:
                SceneController.LoadLevel("Main");
                break;
            case 51:
            case 52:
                SceneController.LoadLevel("BossList");
                break;
            case 61:
                SceneController.LoadLevel("Achievement");
                break;
            case 71:
            case 72:
                SceneController.LoadLevel("Friend");
                break;
            case 81:
                SceneController.LoadLevel("Store");
                break;
        }        
	}

	public void OnClickedCompleteButton()
	{
		NetworkManager.Instance.C2S_GET_QUEST_REWARD(ResponseGetQuestReward, UserData.Instance.userModel.questInfo.questId);
	}

	private void ResponseGetQuestReward(ResponseModel<QuestRewardReponseModel> result)
	{
		Popup_QuestComplete popup = PopupManager.PushPopup(PopupKind.QuestCompleteResult) as Popup_QuestComplete;
		popup.SetItemInfos();
	}

	public override void BackButton ()
	{
		base.BackButton ();
		SceneController.LoadLevel("Main");
	}
}
